#include "StompedReality/Core/Logger.h"
#include "StompedReality/Core/Task/TaskManager.h"
#include "StompedReality/Core/TypeSystem/TypeSystem.h"
namespace StompedReality
{
namespace Core
{
TaskManager::TaskManager( )
{
}

TaskManager::~TaskManager( )
{
  for (ProcessList::iterator iter = this->_processes.begin( );
       iter != this->_processes.end( );
       ++iter) {
    delete *iter;
  }
}

void TaskManager::process( StompedReality::IGame* game ) {
  for (ProcessList::iterator iter = this->_processes.begin( );
       iter != this->_processes.end( );
       ++iter) {
    (*iter)->Exectue( game );
  }
}

bool TaskManager::ParseConfig ( JsonPTR root ) {
  Json::Value jsonProcesses = root->get( "TaskManager", 0 ).get( "Tasks", 0 );

  LogCallFunctionInfo( "TaskManager", "parseConfig" );

  for ( unsigned i = 0; i < jsonProcesses.size( ); ++i ) {
    LogCallFunctionSubInfo( "add Task" )
    LogInfoPair( "name", jsonProcesses[i].get( "type", "" ).asCString( ));

    StompedReality::type_system::TypeInfo* info =
      StompedReality::type_system::TypeSystem::GetTypeByName( jsonProcesses[i].get( "type", "" ).asCString( ) );

    char* proc = new char[info->size];
    info->constructor( proc );
    info->read( jsonProcesses[i], proc );
    ((StompedReality::Core::Task*) proc)->Initialize( );
    this->_processes.push_back( (StompedReality::Core::Task*) proc );
  }
  return true;
}

}
}
